<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/3/1
 * Time: 19:01
 */

namespace app\storage\service;

use app\storage\model\MaterielModel;
use app\storage\model\PurchaseDetailModel;
use think\Session;

class TagService
{

    private $materielModel;
    private $purchaseDetailModel;

    public function __construct()
    {
        $this->materielModel = new MaterielModel();
        $this->purchaseDetailModel = new PurchaseDetailModel();
    }

    public static function createReelIdByMateriel($materielId)
    {
        $purchaseDetailModel = new PurchaseDetailModel();
        $maxReelId = $purchaseDetailModel->lock(true)->where('sub_materiel_id', $materielId)->order('reel_id desc')->value('reel_id');
        $excision = substr($maxReelId, 12, 1);
        $excision = $excision == false ? '-' : $excision;
        $serialNumber = substr($maxReelId, 13, 4);
        if ($excision == '-') {
            $excision = 'A';
            $serialNumber = '0000';
        }

        if (intval($serialNumber) == 9999) {
            if ($excision == 'Z') {
                return ret_template(false, '超过最大复制次数');
            } else {
                ++$excision;
                $serialNumber = '0000';
            }
        }

        $materielModel = new MaterielModel();
        $serialNumber = str_pad(intval($serialNumber + 1), 4, '0', STR_PAD_LEFT);
        $materielInfo = $materielModel->field('sub_materiel_id,sub_materiel_name,format,item_id')
            ->where('sub_materiel_id', $materielId)
            ->find()->toArray();
        $newReelId = date('ymd') . $materielInfo['item_id'] . $excision . $serialNumber;
        $materielInfo['reel_id'] = $newReelId;
        $tagService = new TagService();
        $materielInfo = $tagService->tagTemplate($materielInfo);
        return ret_template(true, '获取成功', $materielInfo);
    }

    public static function createReelIdByReelId($reelId,$isCopy = true)
    {
        $purchaseDetailModel = new PurchaseDetailModel();
        $isCopy == false ? :$reelId = substr_replace($reelId, '_', 12, 1);
        $where['reel_id'] = ['like', $reelId];

        $maxReelIds = $purchaseDetailModel->alias('a')->field('reel_id,a.sub_materiel_id,sub_materiel_name,format,item_id,c.supplier_id,company_name')
            ->join(['iwh_sub_materiel'=>'b'],'a.sub_materiel_id = b.sub_materiel_id')
            ->join(['iwh_supplier'=>'c'],'a.supplier_id = c.supplier_id')
            ->order('reel_id desc')
            ->where($where)
            ->find();//查找第12位最大的reel_id，即 - ，A，B，C一直向后，逐渐变大
        if (empty($maxReelIds)){//$maxReelIds是对象，要先判断它是否为空，然后才能转换成数组$maxReelId
            return ret_template(false, '不存在该物料');
        }
        $maxReelId = $maxReelIds->toArray();
        $excision = substr($maxReelId['reel_id'], 12, 1);//reelId的第十二位，正常情况下为 - ，如果是复制的标签的话，为字母A、B、C......
        if($isCopy){
            $excision = preg_match ("/^[A-Za-z]/", $excision) ? ++$excision : $excision;//第十二位如果是 - ，则不变。如果是字母，则向后递增一位，如 ++A = B
            $excision = $excision == '-' ? 'A' : $excision;//如果第12位 是 - 的话，替换成 A
             if ($excision == 'Z') {
                return ret_template(false, '超过条码最大复制次数');
            }
        }
        $maxReelId['reel_id'] = substr_replace($reelId, $excision, 12, 1);//替换原reel_id的第12位
        $tagService = new TagService();
        $materielInfo = $tagService->tagTemplate($maxReelId);
        return ret_template(true, '获取成功', $materielInfo);
    }


    private function tagTemplate($info)
    {
        $checker = Session::get('name');
        return [
            'reel_id' => $info['reel_id'],
            'sub_materiel_id' => $info['sub_materiel_id'],
            'sub_materiel_name' => $info['sub_materiel_name'],
            'format' => $info['format'],
            'result' => "合格",
            'checker' => $checker,
            'date' => date("Y", time()) . "年" . date("m", time()) . "月" . date("d", time()) . "日",
            'supplier_id' => $info['supplier_id'],
            'company_name' => $info['company_name'],
        ];
    }
}